//
//  BookButton.m
//  i读
//
//  Created by 王枫楠 on 15/12/10.
//  Copyright © 2015年 123. All rights reserved.
//

#import "BookButton.h"
#import "UIButton+WebCache.h"
#import "BookModel.h"

@interface BookButton()

@end

@implementation BookButton

- (UIButton *)bookButton1{
    if (!_bookButton1) {
        _bookButton1 = [[UIButton alloc]initWithFrame:CGRectMake(0,0,100,150)];
        _bookButton1.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        NSLog(@"-----------%@",self);
        _bookButton1.layer.shadowColor = [UIColor blackColor].CGColor;
        _bookButton1.layer.shadowOffset = CGSizeMake(4,4);
        _bookButton1.layer.shadowOpacity = 0.5;
        [_bookButton1 addTarget:self action:@selector(didClickedBookButton:) forControlEvents:UIControlEventTouchUpInside];
        _bookButton1.adjustsImageWhenHighlighted = NO;
        [self addSubview:_bookButton1];
    }
    return _bookButton1;
}

- (UIButton *)bookButton2{
    if (!_bookButton2) {
        _bookButton2 = [[UIButton alloc]initWithFrame:CGRectMake(0,0,100,150)];
        _bookButton2.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        _bookButton2.layer.shadowColor = [UIColor blackColor].CGColor;
        _bookButton2.layer.shadowOffset = CGSizeMake(4,4);
        _bookButton2.layer.shadowOpacity = 0.5;
       _bookButton2.transform = CGAffineTransformMakeRotation(0.35);
        _bookButton2.adjustsImageWhenHighlighted = NO;
         [_bookButton2 addTarget:self action:@selector(didClickedBookButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_bookButton2];
    }
    return _bookButton2;
}

- (UIButton *)bookButton3{
    if (!_bookButton3) {
        _bookButton3 = [[UIButton alloc]initWithFrame:CGRectMake(0,0,100,150)];
        NSLog(@"%@",self);
        _bookButton3.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height/2);
        NSLog(@"%@",_bookButton3);
        _bookButton3.layer.shadowColor = [UIColor blackColor].CGColor;
        _bookButton3.layer.shadowOffset = CGSizeMake(4,4);
        _bookButton3.layer.shadowOpacity = 0.5;
       [_bookButton3 setTransform:CGAffineTransformMakeRotation(-0.35)];
        _bookButton3.adjustsImageWhenHighlighted = NO;
         [_bookButton3 addTarget:self action:@selector(didClickedBookButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_bookButton3];
    }
    return _bookButton3;
}

- (UILabel *)bookLabel{
    
    if (!_bookLabel) {
        _bookLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,30)];
        _bookLabel.center = CGPointMake(self.frame.size.width / 2, 40);
        _bookLabel.textColor = [UIColor whiteColor];
        _bookLabel.textAlignment = NSTextAlignmentCenter;
        _bookLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:26];
        [self addSubview:_bookLabel];
    }
    return _bookLabel;
}

- (instancetype)initWithFrame:(CGRect)frame
                     topImage:(UIImage *)topImage
                  middleImage:(UIImage *)middleImage
                  bottomImage:(UIImage *)bottomImage
                    bookTitle:(NSString *)title {
    self = [super initWithFrame:frame];
    if (self) {
        self.bookLabel.text = title;
        [self.bookButton3 setBackgroundImage:topImage forState:UIControlStateNormal];
        [self.bookButton1 setBackgroundImage:bottomImage forState:UIControlStateNormal];
        [self.bookButton2 setBackgroundImage:middleImage forState:UIControlStateNormal];
        
    }
    return self;
}

#pragma mark - action

- (void)updataBookButtonImageWithtopImage:(UIImage *)topImage bottomImage:(UIImage *)bottomImage middleImage:(UIImage *)middleImage title:(NSString *)title{
    self.bookLabel.text = title;
    [self.bookButton3 setBackgroundImage:topImage forState:UIControlStateNormal];
    [self.bookButton1 setBackgroundImage:bottomImage forState:UIControlStateNormal];
    [self.bookButton2 setBackgroundImage:middleImage forState:UIControlStateNormal];
}

- (void)updataBookButtonImageWithBookList:(NSArray <BookModel *>*)bookList title:(NSString *)title{
    self.bookLabel.text = title;
    if (bookList.count == 0) {
        [self.bookButton1 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card00"] forState:UIControlStateNormal];
        [self.bookButton2 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card01"] forState:UIControlStateNormal];
        [self.bookButton3 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card02"] forState:UIControlStateNormal];
    }else if (bookList.count == 1){
        [self.bookButton1 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card00"] forState:UIControlStateNormal];
        [self.bookButton2 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card01"] forState:UIControlStateNormal];
        if ([bookList firstObject].img) {
            [self.bookButton3 sd_setBackgroundImageWithURL:[NSURL URLWithString:[bookList firstObject].img] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        }
    }else if (bookList.count == 2){
        NSMutableArray *imageArray = [NSMutableArray array];
        for (BookModel *bookModel in bookList) {
            if(bookModel.img == nil){
                [imageArray addObject:@""];
            }
            else {
                [imageArray addObject:bookModel.img];
            }
        }
        [self.bookButton3 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card00"] forState:UIControlStateNormal];
        [self.bookButton2 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[1]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[0]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
    }else{
        NSMutableArray *imageArray = [NSMutableArray array];
        for (BookModel *bookModel in bookList) {
            if(bookModel.img == nil){
                [imageArray addObject:@""];
            }
            else {
                [imageArray addObject:bookModel.img];
            }
        }
        [self.bookButton3 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[2]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton2 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[1]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[0]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
    }

}

- (void)updataShelfBookButtonImageWithBookList:(NSArray <BookModel *>*)bookList title:(NSString *)title{
    self.bookLabel.text = title;
    if (bookList.count == 0) {
        [self.bookButton2 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card00"] forState:UIControlStateNormal];
        [self.bookButton1 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card01"] forState:UIControlStateNormal];
        [self.bookButton3 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card02"] forState:UIControlStateNormal];
    }else if (bookList.count == 1){
        [self.bookButton2 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card00"] forState:UIControlStateNormal];
        [self.bookButton1 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card01"] forState:UIControlStateNormal];
        if ([bookList firstObject].img) {
            [self.bookButton3 sd_setBackgroundImageWithURL:[NSURL URLWithString:[bookList firstObject].img] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        }
    }else if (bookList.count == 2){
        NSMutableArray *imageArray = [NSMutableArray array];
        for (BookModel *bookModel in bookList) {
            if(bookModel.img == nil){
                [imageArray addObject:@""];
            }
            else {
                [imageArray addObject:bookModel.img];
            }
        }
        
        [self.bookButton2 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[1]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[0]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton3 setBackgroundImage:[UIImage imageNamed:@"mpg_pic_card00"] forState:UIControlStateNormal];
    }else{
        NSMutableArray *imageArray = [NSMutableArray array];
        for (BookModel *bookModel in bookList) {
            if(bookModel.img == nil){
                [imageArray addObject:@""];
            }
            else {
                [imageArray addObject:bookModel.img];
            }
        }
        [self.bookButton2 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[2]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[1]]  forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
        [self.bookButton3 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageArray[0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"pic_defaultbook"]];
    }

}

- (void)didClickedBookButton:(UIButton *)button{
    if (self.delegate && [self.delegate respondsToSelector:@selector(bookButtonDidClickedButton:)]) {
        [self.delegate bookButtonDidClickedButton:self];
    }
}

@end
